<?php
/*
Primary Author: Derek Shue
Secondary: Alex Arey, Sean Margison
Updated: 12/11/2011
Module Description: The profile controller is responsible for profile management (view/edit)
for all user types and for community partner contact management.
*/
class ProfileController extends Zend_Controller_Action
{
	/**
	* Init checks user session exists and redirects otherwise.
	*/
    public function init()
    {
        $auth = Zend_Auth::getInstance(); 
		if($auth->hasIdentity()){
			$partnerID = $auth->getIdentity()->getUID();
		}else{
			$this->_helper->redirector('index','index');
			return;
		}
    }

    public function indexAction()
    {
		return $this->_helper->redirector('index','index');
    }


	/**
		Handles user-type based submit by calling the appropriate function.
	*/
    public function submitAction()
    {
		$switch = $this->getRequest()->getParam("type");
		

		if($switch == "teacher")
        	$this->TeacherAdd();
		
		if($switch == "student")
		$this->StudentAdd();
		
		if($switch == "organization")
		$this->OrganizationAdd();
		
	
	}
	
	/**
	* Handles user-type conditioned profile edits by calling appropriate function.
	*/
	public function editAction()
    {
		//$switch = $this->getRequest()->getParam("type");
		$auth = Zend_Auth::getInstance();
		$type = $auth->getIdentity()->getUserType();

		//if($switch == "teacher")
		if($type == 'teacher')
        	$this->TeacherEdit();
		
		//if($switch == "student")
		if($type == 'student')
			$this->StudentEdit();
		
		//if($switch == "organization")
		if($type == 'organization')
		$this->OrganizationEdit();
        
    }
	
	/**
	* Provides display information for community partner contacts.
	*/
	public function managecontactsAction(){
		$auth = Zend_Auth::getInstance(); //verify login and user type.
		if($auth->hasIdentity()&&$auth->getIdentity()->getUserType()=='organization'){
			$partnerID = $auth->getIdentity()->getUID();
		}else{
			$this->_helper->redirector('index','index');
			return;
		}

		$contacts = new Application_Model_DbTable_CpContacts();
		$contactsQuery = new Application_Model_ContactsQuery();
		$contactlist = $contactsQuery->getContacts_CP($partnerID)->toArray();
		$this->view->contacts = $contactlist;
	}
	
	/**
	* Designates the primary contact for the community partner
	*/
	public function setprimarycontactAction(){
		$auth = Zend_Auth::getInstance(); //verify login and user type.
		if($auth->hasIdentity()&&$auth->getIdentity()->getUserType()=='organization'){
			$partnerID = $auth->getIdentity()->getUID();
		}else{
			$this->_helper->redirector('index','index');
			return;
		}

        if ($this->getRequest()->isPost()) {
			$set = $this->getRequest()->getPost('set');
            
            if($set=='Yes') {
				$primary = $this->getRequest()->getPost('primary');
                $cpcontacts = new Application_Model_DbTable_CpContacts();
				try{
					$cpcontacts->changePrimaryContact((int)$primary, $partnerID);
				}
				catch(Exception $e){
					return null;
				}
					
            }

           $this->_helper->redirector('managecontacts', 'profile');
        } 
		else {
            $contactid = $this->_getParam('contactid');
            $contacts = new Application_Model_DbTable_CpContacts();
            $this->view->contact = $contacts->getCPContact("CONTACTID = ".$contactid);
        }
	}
	
	/**
	* Deletes a community partner contact
	*/
	public function deletecontactAction()
    {
		$auth = Zend_Auth::getInstance(); 
		if($auth->hasIdentity()&&$auth->getIdentity()->getUserType()=='organization'){
			$partnerID = $auth->getIdentity()->getUID();
		}else{
			$this->_helper->redirector('index','index');
			return;
		}

        if ($this->getRequest()->isPost()) {
            $del = $this->getRequest()->getPost('del');
            if ($del == 'Yes') {
                $id = $this->getRequest()->getPost('contactid');
                $contacts = new Application_Model_DbTable_CpContacts();
				$contacts->deleteCPContact("CONTACTID = " . $id);
		
            }
			
          $this->_helper->redirector('index');
        } else {
            $contactid = $this->_getParam('contactid');
            $contacts = new Application_Model_DbTable_CpContacts();
			$this->view->contact = $contacts->getCPContact("CONTACTID = ".$contactid);
        }
    }
	
	/**
	* Processes the adding of a community partner contact
	*/
	public function addcontactAction(){
		$auth = Zend_Auth::getInstance(); 
		if($auth->hasIdentity()&&$auth->getIdentity()->getUserType()=='organization'){
			$partnerID = $auth->getIdentity()->getUID();
		}else{
			$this->_helper->redirector('index','index');
			return;
		}
		$form = new Application_Form_CpContacts();
		$this->view->form = $form;
		$contacts = new Application_Model_DbTable_CpContacts();
	
		if ($this->getRequest()->isPost()){
		    $formData = $this->getRequest()->getPost();
		    if ($form->isValid($formData)) {
				$term = new Application_Model_Term();
				

				$cpid = $partnerID;
				$prefix = $form->getValue('PREFIX');
				$firstname = $form->getValue('FIRSTNAME');
				$lastname = $form->getValue('LASTNAME');
				$suffix = $form->getValue('SUFFIX');
				$gender = $form->getValue('GENDER');
				$jobtitle = $form->getValue('JOBTITLE');
				$phone = $form->getValue('PHONE');
				$extension = $form->getValue('EXTENSION');
				$fax = $form->getValue('FAX');
				$address1 = $form->getValue('ADDRESS1');
				$address2 = $form->getValue('ADDRESS2');
				$city = $form->getValue('CITY');
				$state = $form->getValue('STATE');
				$zip = $form->getValue('ZIP');
				$email = $form->getValue('EMAIL');
				
				try{
					$contacts->addCPContact($cpid,$prefix,$firstname,$lastname,$suffix,$gender,$jobtitle,$phone,$extension,$fax,$address1,$address2,$city,$state,$zip,$email);
					return $this->_helper->redirector('managecontacts','profile');
				}
				catch(Exception $e){
					
				}
				return null;						 
		    }
		}

	}
	
	/**
	* Processes the editing of a community partner contact.
	*/
	public function editcontactAction(){
	
		$auth = Zend_Auth::getInstance(); 
		if($auth->hasIdentity()&&$auth->getIdentity()->getUserType()=='organization'){
			$partnerID = $auth->getIdentity()->getUID();
		}else{
			$this->_helper->redirector('index','index');
			return;
		}
	
		$form = new Application_Form_CpContacts();
		$form->submit->setLabel('Update');
		$this->view->form = $form;
		$contacts = new Application_Model_DbTable_CpContacts();
		
	
	
        if ($this->getRequest()->isPost()){
			$formData = $this->getRequest()->getPost();
		    if ($form->isValid($formData)) {
				$contactid = $form->getValue('CONTACTID');
				$cpid = $partnerID;
				$prefix = $form->getValue('PREFIX');
				$firstname = $form->getValue('FIRSTNAME');
				$lastname = $form->getValue('LASTNAME');
				$suffix = $form->getValue('SUFFIX');
				$gender = $form->getValue('GENDER');
				$jobtitle = $form->getValue('JOBTITLE');
				$phone = $form->getValue('PHONE');
				$extension = $form->getValue('EXTENSION');
				$fax = $form->getValue('FAX');
				$address1 = $form->getValue('ADDRESS1');
				$address2 = $form->getValue('ADDRESS2');
				$city = $form->getValue('CITY');
				$state = $form->getValue('STATE');
				$zip = $form->getValue('ZIP');
				$email = $form->getValue('EMAIL');
				
				try{
					$contacts->updateCPContact($contactid,$cpid,$prefix,$firstname,$lastname,$suffix,$gender,$jobtitle,$phone,$extension,$fax,$address1,$address2,$city,$state,$zip,$email);
					$this->_helper->redirector('index');
				}
				catch(Exception $e){
					echo $e->getMessage();
				}
				return null;						 
		    }
		}
		else {
            $id = $this->_getParam('contactid');
            if ($id > 0) {
                $form->populate($contacts->getCPContact("CONTACTID = ".$id));
	        }
        }
	
	
	}
	
	
	/**
	* Processes the creation of a community partner profile
	* this action binds the user to the 'Organizations' database
	*/
	public function OrganizationAdd(){
	
	$form = new Application_Form_OrganizationProfile();
	$form->submit->setLabel('Add Profile');
    $this->view->form = $form;
    
	if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {							
				$auth = Zend_Auth::getInstance(); 
				if($auth->getIdentity())
					{
						$identity = $auth->getIdentity();
						$id = $identity->getUID();
					}
				else{
						$this->_helper->redirector('index', 'index');
					}	
							
				$state = $form->getValue('STATE');
				$zip = $form->getValue('ZIP');
				$phone = $form->getValue('PHONE');
				$extension = $form->getValue('EXTENSION');
				$fax = $form->getValue('FAX');
				$otherphone = $form->getValue('OTHERPHONE');
				$check_website = $form->getValue('WEBSITE');
				if(substr($check_website,0,7)=="http://"){
					$website = $form->getValue('WEBSITE');
				}else{
					$website = "http://".$check_website;
				}
				$mission = $form->getValue('MISSION');
				$description = $form->getValue('DESCRIPTION');
				
				//$transport = $form->getValue('TRANSPORT');
				
				$name = $form->getValue('NAME');
				$address1 = $form->getValue('ADDRESS1');
				$address2 = $form->getValue('ADDRESS2');
				$city = $form->getValue('CITY');
				$county = $form->getValue('COUNTY');

				
                $organizations = new Application_Model_DbTable_Organizations();
                $organizations->addOrganization($id, $state, $zip, $phone, $extension, $fax, $otherphone, $website, $mission, $description, $transport, $name, $address1, $address2, $city, $county);
               			
				$users = new Application_Model_DbTable_Users();
				$currentinfo = $users->getUser("ID = '" . $id . "'");	
				$users->updateUser($currentinfo['ID'], $currentinfo['LOGINNAME'] , "", max(1,$userrank));
				$identity->setUserRank(max(1,$userrank));
				
				
				$this->_helper->redirector('index', 'index');
				
            } else {
                $form->populate($formData);
            }
        }
	
	}
	
	/**
	* Update for community partner profile data.
	*/
	public function OrganizationEdit(){
	
		$auth = Zend_Auth::getInstance(); 
		if($auth->getIdentity())
			{
				$identity = $auth->getIdentity();
				$id = $identity->getUID();
			}
		else{
				$this->_helper->redirector('index', 'index');
				return;
			}
	
			$contactquery = new Application_Model_ContactsQuery();
			$contact = $contactquery->getPrimaryContact_CP($id);
			$this->view->contact = $contact; 
			
			$form = new Application_Form_OrganizationProfile();
			$form->submit->setLabel('Update Profile');
        	$this->view->form = $form;

			
		if ($this->getRequest()->isPost()) {
			$formData = $this->getRequest()->getPost();
   			if ($form->isValid($formData)){
							
				$state = $form->getValue('STATE');
				$zip = $form->getValue('ZIP');
				$phone = $form->getValue('PHONE');
				$extension = $form->getValue('EXTENSION');
				$fax = $form->getValue('FAX');
				$otherphone = $form->getValue('OTHERPHONE');
				$check_website = $form->getValue('WEBSITE');
				if(substr($check_website,0,7)=="http://"){
					$website = $form->getValue('WEBSITE');
				}else{
					$website = "http://".$check_website;
				}
				$mission = $form->getValue('MISSION');
				$description = $form->getValue('DESCRIPTION');
				//$transport = $form->getValue('TRANSPORT');
				$name = $form->getValue('NAME');
				$address1 = $form->getValue('ADDRESS1');
				$address2 = $form->getValue('ADDRESS2');
				$city = $form->getValue('CITY');
				$county = $form->getValue('COUNTY');
				
	
                $organizations = new Application_Model_DbTable_Organizations();
                $organizations->updateOrganization($id, $state, $zip, $phone, $extension, $fax, $otherphone, $website, $mission, $description, $transport, $name, $address1, $address2, $city, $county);
				
				$this->_helper->redirector('index', 'index');
				
				}
			}
			else {
            
            if ($id > 0) {
                $organizations = new Application_Model_DbTable_Organizations();
                $form->populate($organizations->getOrganization("ID = ".$id));
						
            }
        }
		
		
	
	}
	/**
	* Create student profile. Binds user to STUDENTS database
	*/
	public function StudentAdd(){
	
	    $form = new Application_Form_StudentProfile();
        $form->submit->setLabel('Add Profile');
        $this->view->form = $form;
        
		
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
		$auth = Zend_Auth::getInstance(); 
			if($auth->getIdentity()){
				$identity = $auth->getIdentity();
					$id = $identity->getUID();
					$userrank = $identity->getUserRank();
				}
				else{
					$id = 0;
					$userrank = 0;
				}	
							
				$pid = $form->getValue('PID');
				$firstname = $form->getValue('FIRSTNAME');
				$middlename = $form->getValue('MIDDLENAME');
				$lastname = $form->getValue('LASTNAME');
				$email = $form->getValue('EMAIL');
				$phone = $form->getValue('PHONE');
				$major = $form->getValue('MAJOR');
				$studentstate = $form->getValue('STUDENTSTATE');
				$howhear = $form->getValue('HOWHEAR');


			
                $students = new Application_Model_DbTable_Students();
                $students->addStudent($id, $pid, $firstname, $middlename, $lastname, $email, $phone, $major, $studentstate, $howhear);
                
								
				$users = new Application_Model_DbTable_Users();
				$currentinfo = $users->getUser("ID = '" . $id . "'");	
				$users->updateUser($currentinfo['ID'], $currentinfo['LOGINNAME'] , "", max(1,$userrank));
				$identity->setUserRank(max(1,$userrank));
						
						
				$this->_helper->redirector('index', 'index');
				
            } else {
                $form->populate($formData);
            }
        }
	
	}
	
	/**
	* edit student profile information.
	*/
	public function StudentEdit(){
	
	
		$auth = Zend_Auth::getInstance(); 
		if($auth->getIdentity())
			{
				$identity = $auth->getIdentity();
				$id = $identity->getUID();
				$userrank = $identity->getUserRank();
			}
		else{
				$id = 0;
				$userrank = 0;
			}	
	
		$form = new Application_Form_StudentProfile();
        $form->submit->setLabel('Update Profile');
        $this->view->form = $form;
        
		
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
							
		
				$pid = $form->getValue('PID');
				$firstname = $form->getValue('FIRSTNAME');
				$middlename = $form->getValue('MIDDLENAME');
				$lastname = $form->getValue('LASTNAME');
				$email = $form->getValue('EMAIL');
				$phone = $form->getValue('PHONE');
				$major = $form->getValue('MAJOR');
				$studentstate = $form->getValue('STUDENTSTATE');
				$howhear = $form->getValue('HOWHEAR');

		
                $students = new Application_Model_DbTable_Students();
                $students->updateStudent($id,$pid, $firstname, $middlename, $lastname, $email, $phone, $major, $studentstate, $howhear);
				$this->_helper->redirector('index', 'index');
				
            }
			}
			else {
            
            if ($id > 0) {
                $students = new Application_Model_DbTable_Students();
                $form->populate($students->getStudent("ID = ".$id));
						
            }
        }
	
	
	}
	/**
	* Creates teacher profile. Binds the user to the teachers database.
	*/
	public function TeacherAdd(){
	
	 $form = new Application_Form_TeacherProfile();
        $form->submit->setLabel('Add Profile');
        $this->view->form = $form;
        
		
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
				
				$auth = Zend_Auth::getInstance(); 
				if($auth->getIdentity())
					{
						$identity = $auth->getIdentity();
						$id = $identity->getUID();
						$userrank = $identity->getUserRank();
					}
				else{
						$id = 0;
						$userrank = 0;
					}	
							
				$firstname = $form->getValue('FIRSTNAME');
				$lastname = $form->getValue('LASTNAME');
				$department = $form->getValue('DEPARTMENT');
				$officelocation = $form->getValue('OFFICELOCATION');
				$email = $form->getValue('EMAIL');

				
                $teachers = new Application_Model_DbTable_Teachers();
                $teachers->addTeacher($id,$firstname, $lastname, $department, $officelocation, $email);
           
		   
				
				$users = new Application_Model_DbTable_Users();
				$currentinfo = $users->getUser("ID = '" . $id . "'");	
				$users->updateUser($currentinfo['ID'], $currentinfo['LOGINNAME'] , "", max(1,$userrank));
				$identity->setUserRank(max(1,$userrank));
				
				
				
				$this->_helper->redirector('index', 'index');
				
            } else {
                $form->populate($formData);
            }
        }
	
	
	}
	/**
	* Update for teacher profile information
	*/
	public function TeacherEdit(){
	
	$auth = Zend_Auth::getInstance(); 
		if($auth->getIdentity())
			{
				$identity = $auth->getIdentity();
				$id = $identity->getUID();
				$userrank = $identity->getUserRank();
			}
		else{
				$id = 0;
				$userrank = 0;
			}	
	
		$form = new Application_Form_TeacherProfile();
        $form->submit->setLabel('Update Profile');
        $this->view->form = $form;
        
		
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->getPost();
            if ($form->isValid($formData)) {
							
		
				$firstname = $form->getValue('FIRSTNAME');
				$lastname = $form->getValue('LASTNAME');
				$department = $form->getValue('DEPARTMENT');
				$officelocation = $form->getValue('OFFICELOCATION');
				$email = $form->getValue('EMAIL');
	
                $teachers = new Application_Model_DbTable_Teachers();
                $teachers->updateTeacher($id,$firstname, $lastname, $department, $officelocation, $email);
				$this->_helper->redirector('index', 'index');
				
            }
			}
			else {
            
            if ($id > 0) {
                $teachers = new Application_Model_DbTable_Teachers();
                $form->populate($teachers->getTeacher("ID = ".$id));
						
            }
        }	
	}
    
    
    
}






